package com.mikepenz.app.activity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.mikepenz.app.R;
import com.mikepenz.bean.CarBean;
import com.mikepenz.presenter.SendOrderPresenter;
import com.mikepenz.presenter.imp.SendOrderPresenterImp;
import com.mikepenz.view.SendOrderView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.until.AlertHelper;

import butterknife.InjectView;
import butterknife.OnClick;


/**
 * 生成租车订单
 */

public class OrderActivity extends BaseActivity implements View.OnClickListener
        , AdapterView.OnItemSelectedListener, SendOrderView, DatePickerDialog.OnDateSetListener {


    ProgressDialog loading;
    DatePickerDialog datePicker;
    @InjectView(R.id.send_order_car_type)
    TextView sendOrderCarType;
    @InjectView(R.id.send_order_type)
    TextView sendOrderType;
    @InjectView(R.id.send_order_spinner)
    Spinner sendOrderSpinner;
    @InjectView(R.id.send_order_start_time)
    TextView sendOrderStartTime;
    @InjectView(R.id.send_order_end_time)
    TextView sendOrderEndTime;
    @InjectView(R.id.send_order_time_linear)
    LinearLayout sendOrderTimeLinear;
    @InjectView(R.id.send_order_description)
    EditText sendOrderDescription;
    @InjectView(R.id.order_submit)
    Button orderSubmit;

    SendOrderPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Handle Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }


    @Override
    public int getLayoutID() {
        return R.layout.activity_order2;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initActionbar() {
        getSupportActionBar().setTitle(R.string.submit_order);
    }

    @Override
    public void initData() {
        presenter = new SendOrderPresenterImp(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_submit:
//                Intent intent = new Intent(OrderActivity.this, RendingActivity.class);
//                startActivity(intent);
                if (!presenter.checkText(sendOrderDescription.getText().toString())) return;
                presenter.submit(sendOrderSpinner.getSelectedItemPosition(),
                        sendOrderStartTime.getText().toString(), sendOrderEndTime.getText().toString(),
                        sendOrderDescription.getText().toString());
                break;
        }

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    @Override
    public void initViewOrEvent(CarBean carBean) {
        AlertHelper helper = new AlertHelper(this);
        loading = helper.LoadingAlert();
        datePicker = helper.dateAlert(this);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.rent_type, android.R.layout.simple_spinner_item);
        sendOrderSpinner.setAdapter(adapter);
        sendOrderSpinner.setOnItemSelectedListener(this);
        orderSubmit.setOnClickListener(this);
        sendOrderCarType.setText(carBean.getCarType());
    }

    @Override
    public void showAlert() {
        if (loading != null && !loading.isShowing())
            loading.show();
    }

    @Override
    public void dismissAlert() {
        if (loading != null && loading.isShowing())
            loading.dismiss();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void setTextError(String error) {
        sendOrderDescription.setError(error);
    }

    @Override
    public void timeLinearStatue(boolean statue) {
        if (statue)
            sendOrderTimeLinear.setVisibility(View.VISIBLE);
        else
            sendOrderTimeLinear.setVisibility(View.GONE);
    }

    @OnClick(R.id.send_order_start_time)
    public void showDate() {
        if (datePicker != null) datePicker.show();
        presenter.checkTime(0);
    }

    @OnClick(R.id.send_order_end_time)
    public void showEndDate() {
        if (datePicker != null) datePicker.show();
        presenter.checkTime(1);
    }

    public void dismissDate() {
        if (datePicker != null)
            datePicker.dismiss();
    }

    @Override
    public void setStartTime(String time) {
        sendOrderStartTime.setText(time);
    }

    @Override
    public void setEndTime(String time) {
        sendOrderEndTime.setText(time);

    }

    @Override
    public void onDateSet(DatePicker datePicker, int year, int mouth, int day) {
        if(mouth<10 && day>10)
            presenter.bindTime(year + "-0" + mouth + "-" + day);
        else if (day<10 && mouth<10)
            presenter.bindTime(year + "-0" + mouth + "-0" + day);
        else if (day<10 && mouth>10)
            presenter.bindTime(year + "-" + mouth + "-0" + day);
        else
            presenter.bindTime(year + "-" + mouth + "-" + day);
    }
}
